<template>
  <el-dialog v-model="visible" title="问题上报" width="600px">
    <el-form label-width="80px">
      <el-form-item label="问题类型" required>
        <el-select v-model="form.type" placeholder="选择问题类型">
          <el-option
            v-for="type in issueTypes"
            :key="type.value"
            :label="type.label"
            :value="type.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="严重程度" required>
        <el-radio-group v-model="form.severity">
          <el-radio-button label="low">低</el-radio-button>
          <el-radio-button label="medium">中</el-radio-button>
          <el-radio-button label="high">高</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="问题描述" required>
        <el-input
          v-model="form.description"
          type="textarea"
          :rows="4"
          placeholder="请详细描述问题现象"
        />
      </el-form-item>

      <el-form-item label="上传附件">
        <el-upload
          multiple
          :limit="5"
          :on-exceed="handleExceed"
          :before-upload="beforeUpload"
        >
          <el-button type="primary">点击上传</el-button>
          <template #tip>
            <div class="upload-tip">支持上传图片/文档，单个文件不超过10MB</div>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submit">提交报告</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useProjectStore } from '@/stores/project'
import { ElMessage } from 'element-plus'

const props = defineProps({
  modelValue: Boolean,
  projectId: Number
})

const emit = defineEmits(['update:modelValue'])

const visible = ref(props.modelValue)
const projectStore = useProjectStore()

const form = ref({
  type: '',
  severity: 'medium',
  description: '',
  attachments: []
})

const issueTypes = ref([
  { value: 'safety', label: '安全隐患' },
  { value: 'quality', label: '质量问题' },
  { value: 'schedule', label: '进度问题' },
  { value: 'other', label: '其他问题' }
])

watch(
  () => props.modelValue,
  (val) => {
    visible.value = val
  }
)

watch(visible, (val) => {
  emit('update:modelValue', val)
})

const handleExceed = () => {
  ElMessage.warning('最多上传5个文件')
}

const beforeUpload = (file) => {
  const isAllowedType = ['image/png', 'image/jpeg', 'application/pdf'].includes(
    file.type
  )
  const isLt10M = file.size / 1024 / 1024 < 10

  if (!isAllowedType) {
    ElMessage.error('只支持PNG、JPG和PDF格式')
    return false
  }
  if (!isLt10M) {
    ElMessage.error('文件大小不能超过10MB')
    return false
  }
  return true
}

const submit = () => {
  projectStore.reportIssue(props.projectId, form.value)
  ElMessage.success('问题上报成功')
  visible.value = false
  form.value = {
    type: '',
    severity: 'medium',
    description: '',
    attachments: []
  }
}
</script>

<style scoped>
.upload-tip {
  color: #999;
  font-size: 12px;
  margin-top: 8px;
}
</style>
